package ocean.chinacraft.common.util;

import net.minecraft.entity.EntityPredicate;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.crafting.AbstractCookingRecipe;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.common.ForgeHooks;

import java.util.List;
import java.util.Random;

/**
 * Mod工具类
 *
 * @author zhang
 * @since 2021/11/17
 */
public class ModUtil {

    /**
     * 是否是燃料、可燃烧
     *
     * @param itemStack   物品堆
     * @param iRecipeType 配方类型
     * @return 结果
     */
    public static boolean isFuel(ItemStack itemStack, IRecipeType<? extends AbstractCookingRecipe> iRecipeType) {
        return ForgeHooks.getBurnTime(itemStack, iRecipeType) > 0 || itemStack.getItem() == Items.LAVA_BUCKET;
    }

    /**
     * 是否是燃料、可燃烧
     *
     * @param itemStack 物品堆
     * @return 结果
     */
    public static boolean isFuel(ItemStack itemStack) {
        return ForgeHooks.getBurnTime(itemStack, IRecipeType.SMELTING) > 0 || itemStack.getItem() == Items.LAVA_BUCKET;
    }

    /**
     * 获取燃烧时间
     *
     * @param itemStack   物品堆
     * @param iRecipeType 配方类型
     * @return 结果
     */
    public static int getBurnTime(ItemStack itemStack, IRecipeType<? extends AbstractCookingRecipe> iRecipeType) {
        return ForgeHooks.getBurnTime(itemStack, iRecipeType);
    }

    /**
     * 获取燃烧时间
     *
     * @param itemStack 物品堆
     * @return 结果
     */
    public static int getBurnTime(ItemStack itemStack) {
        return ForgeHooks.getBurnTime(itemStack, IRecipeType.SMELTING);
    }

    /**
     * 生成1-100的随机数
     *
     * @return 结果
     */
    public static Integer random() {
        return (int) (Math.random() * (100 - 1) + 1);
    }

    /**
     * 生成指定随机数
     *
     * @return 结果
     */
    public static Integer random(int max, int min) {
        return new Random().nextInt(max - min + 1) + min;
    }

    /**
     * Object数组转int数组
     *
     * @param target 目标字符
     * @return 结果
     */
    public static Integer[] getIntArray(Object[] target) {
        Integer[] ret = new Integer[target.length];
        for (int i = 0; i < target.length; i++) {
            ret[i] = Integer.parseInt(String.valueOf(target[i]));
        }
        return ret;
    }

    public static boolean isNotNull(ItemStack... items) {
        for (ItemStack item : items) {
            if (item == null || !(item.getItem() instanceof ArmorItem)) {
                return false;
            }
        }
        return true;
    }

    public static List<MobEntity> getNearbyEntities(PlayerEntity player, int range, EntityPredicate predicate) {
        double x = player.getX();
        double y = player.getY();
        double z = player.getZ();
        return player.level.getNearbyEntities(MobEntity.class, predicate, player,
                new AxisAlignedBB(x - range, y - range, z - range
                        , x + range, y + range, z + range));
    }
}
